//
//  vdMessageButton.m
//  Void Dead
//
//  Created by Sidney Just on 08.12.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdMessageButton.h"


@implementation vdMessageButton

#pragma mark -
#pragma mark Touch

- (void)getTouch:(CGPoint)point {	
	if(enabled)
	{
		if((point.x >= aPos_x && point.x <= aPos_x + size_x) && (point.y >= aPos_y && point.y <= aPos_y + size_y))
		{
			if(statePressed)
			{
				[self setTexture:statePressed];
				
				pressed = YES;
			}
			
			if(statePressedLabel)
			{
				statePressedLabel.flags |= SHOW;
				statePressedLabel.pos_x = ((size_x / 2) - (statePressedLabel.size_x / 2));
				statePressedLabel.pos_y = ((size_y / 2) - (statePressedLabel.size_y / 2));
			}
			
			if(stateNormalLabel)
				stateNormalLabel.flags &= ~ SHOW;
			
			if(stateDisabledLabel)
				stateDisabledLabel.flags &= ~ SHOW;
			
		} else {
			if(stateNormal)
			{
				[self setTexture:stateNormal];
				pressed = NO;
			}
			
			if(stateNormalLabel)
			{
				stateNormalLabel.flags |= SHOW;
				stateNormalLabel.pos_x = ((size_x / 2) - (stateNormalLabel.size_x / 2));
				stateNormalLabel.pos_y = ((size_y / 2) - (stateNormalLabel.size_y / 2));
			}
			
			if(statePressedLabel)
				statePressedLabel.flags &= ~ SHOW;
			
			if(stateDisabledLabel)
				stateDisabledLabel.flags &= ~ SHOW;
		}
	} else {
		[self setTexture:stateDisabled];
		
		if(stateDisabledLabel)
		{
			stateDisabledLabel.flags |= SHOW;
			stateDisabledLabel.pos_x = ((size_x / 2) - (stateDisabledLabel.size_x / 2));
			stateDisabledLabel.pos_y = ((size_y / 2) - (stateDisabledLabel.size_y / 2));
		}
		
		if(stateNormalLabel)
			stateNormalLabel.flags &= ~ SHOW;
		
		if(statePressedLabel)
			statePressedLabel.flags &= ~ SHOW;
	}
}

- (void)endSequence {
	if(enabled)
	{
		if(stateNormal)
		{
			[self setTexture:stateNormal];
			pressed = NO;
		}
		
		if(pressed && pressedDelegate)
		{
			if([pressedDelegate conformsToProtocol:@protocol(vdInterfaceProtocol)])
			{
				if([pressedDelegate respondsToSelector:@selector(buttonTouchUpInside:)])
					[pressedDelegate buttonTouchUpInside:self];
			}
		}
	} else {
		[self setTexture:stateDisabled];
	}
}

#pragma mark -
#pragma mark Setter

- (void)setNormalStateMessage:(NSString *)msg {
	if(!stateNormalLabel)
	{
		stateNormalLabel = [vdLabel labelWithText:msg];
		[self addChild:stateNormalLabel];
		
		[self getTouch:CGPointMake(pos_x + size_x + 5, pos_y + size_y + 5)];
	} else {
		[stateNormalLabel setString:msg];
	}
	
	if(!pressed)
	{
		statePressedLabel.flags |= SHOW;
	} else {
		statePressedLabel.flags &= ~ SHOW;
	}

}

- (void)setPressedStateMessage:(NSString *)msg {
	if(!statePressedLabel)
	{
		statePressedLabel = [vdLabel labelWithText:msg];
		[self addChild:statePressedLabel];
		[self getTouch:CGPointMake(pos_x + size_x + 5, pos_y + size_y + 5)];
	} else {
		[statePressedLabel setString:msg];
	}
	
	if(pressed)
	{
		statePressedLabel.flags |= SHOW;
	} else {
		statePressedLabel.flags &= ~ SHOW;
	}
}

- (void)setDisabledStateMessage:(NSString *)msg {
	if(!stateDisabledLabel)
	{
		stateDisabledLabel = [vdLabel labelWithText:msg];
		[self addChild:stateDisabledLabel];
		[self getTouch:CGPointMake(pos_x + size_x + 5, pos_y + size_y + 5)];
	} else {
		[stateDisabledLabel setString:msg];
	}
	
	if(!enabled)
	{
		stateDisabledLabel.flags |= SHOW;
	} else {
		stateDisabledLabel.flags &= ~ SHOW;
	}
}

- (id)initWithNormalState:(NSString	*)nstateTex withMessage:(NSString *)nstateMess andPressedState:(NSString *)pstateTex withMessage:(NSString *)pstateMess {
	if(self = [super initWithNormalState:nstateTex andPressedState:pstateTex])
	{
		stateNormalLabel = NULL;
		statePressedLabel = NULL;
		stateDisabledLabel = NULL;
		
		[self setNormalStateMessage:nstateMess];
		[self setPressedStateMessage:pstateMess];
	}
	return self;
}

- (id)init {
	if(self = [super init])
	{
		stateNormalLabel = NULL;
		statePressedLabel = NULL;
		stateDisabledLabel = NULL;
	}
	return self;
}

- (void)dealloc {
	[self cleanChildren];
	
	[super dealloc];
}

@end
